package com.epalmpay.controller.ys.oilcard;


import java.io.PrintWriter;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.epalmpay.commom.BaseController;
import com.epalmpay.entity.YsOilcardOrder;
import com.epalmpay.service.weixin.IWxApiWalletService;
import com.epalmpay.service.yunstream.IOilcardOrderService;
import com.epalmpay.util.SysUtils;





@Controller
@RequestMapping(value = "/oilCardRecharge/recharge")
public class OilCardRechNotifyController extends BaseController{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = LoggerFactory.getLogger(OilCardRechNotifyController.class);
	
	  @Resource
		private IWxApiWalletService wxApiWalletServiceService;
	  
	  
	@Resource
	private IOilcardOrderService oilcardOrderService;
	 /**
	    * 油卡充值1.0 的回调
	    * @param request
	    * @param response
	    * @throws Exception
	    */
	@RequestMapping("callBack")
	public void callBack(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String outTradeNo = new String(request.getParameter("outTradeNo"));
		String orderId = new String(request.getParameter("orderId"));
		String createTime = new String(request.getParameter("createTime"));
		int status = Integer.valueOf(request.getParameter("status"));
		logger.info("oilCard recharge callBack info : outTradeNo="+outTradeNo+";orderId="+orderId);
		if(oilcardOrderService.checkOilChargeSign(request)){
			logger.info("oilCard recharge callBack checkSign success");
			
			YsOilcardOrder oilcardOrder =oilcardOrderService.rechargeCallBack(outTradeNo, orderId, createTime, status,null);
			if(oilcardOrder!=null){
				wxApiWalletServiceService.finishSendYKSuccessMessage(oilcardOrder);
			}
			logger.info("oilCard recharge callBack update order success");
			PrintWriter out = response.getWriter();
			out.write("success");
			out.close();
		}else{
			PrintWriter out = response.getWriter();
			out.write("fail  verify error");
			out.close();
		}
	}

	
	/**
	    * 油卡充值1.0 的回调
	    * @param request
	    * @param response
	    * @throws Exception
	    */
	@RequestMapping("adminCallBack")
	public void adminCallBack(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String outTradeNo = new String(request.getParameter("outTradeNo"));
		String orderId = new String(request.getParameter("orderId"));
		String createTime = new String(request.getParameter("createTime"));
		int status = Integer.valueOf(request.getParameter("status"));
		logger.info("oilCard recharge callBack info : outTradeNo="+outTradeNo+";orderId="+orderId);
		String adminKey= request.getParameter("adminKey");
		if (status==3&&"admin".equals(adminKey)) {
			logger.info("oilCard recharge callBack checkSign success");
			
			YsOilcardOrder oilcardOrder=oilcardOrderService.rechargeCallBack(outTradeNo, orderId, createTime, status,null);
		 	if(oilcardOrder!=null){
				wxApiWalletServiceService.finishSendYKSuccessMessage(oilcardOrder);
			}
			logger.info("oilCard recharge callBack update order success");
			PrintWriter out = response.getWriter();
			out.write("success");
			out.close();
		}else{
			PrintWriter out = response.getWriter();
			out.write("fail  verify error");
			out.close();
		}
	}

	
   /**
    * 油卡充值2.0 的回调
    * @param request
    * @param response
    * @throws Exception
    */
	//http://xxxxxxx/xxx?userId=1&bizId=200&ejId=123188&downstreamSerialno=123188&status=2&sign=65b015dc3945c4cb677e0d39cea17237
	

	@RequestMapping("newCallBack")
	public void newCallBack(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String userId = request.getParameter("userId");
		String bizId = request.getParameter("bizId");
		String ejId =request.getParameter("ejId");
		String outTradeNo =request.getParameter("downstreamSerialno");
		String ext =request.getParameter("ext");
		String memo =request.getParameter("memo");
		String status = request.getParameter("status");
		logger.info("oilCard recharge callBack info : outTradeNo="+outTradeNo+";orderId="+ejId);
		if(oilcardOrderService.checkOilChargeSign2(request)){
			logger.info("oilCard recharge callBack checkSign success");
			String createTime=SysUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
			//2.0中status 状态 2 是成功 3 是失败 ，1,0中3是成功 4及其以上为失败需要转化
			if(SysUtils.Object2int(status)==2){
				YsOilcardOrder oilcardOrder=oilcardOrderService.rechargeCallBack(outTradeNo, ejId, createTime , 3,memo);
				if(oilcardOrder!=null){
					wxApiWalletServiceService.finishSendYKSuccessMessage(oilcardOrder);
				}
				logger.info("oilCard recharge callBack update order success");
			}else{
				oilcardOrderService.rechargeCallBack(outTradeNo, ejId, createTime , 2,memo);
				logger.info("oilCard recharge callBack update order  fail");
			}
			
			PrintWriter out = response.getWriter();
			out.write("success");
			out.close();
		}else{
			PrintWriter out = response.getWriter();
			out.write("fail  verify error");
			out.close();
		}
	}
}
